﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;
using Silverlight.Weblog.Client.DAL.Events;
using Silverlight.Weblog.Client.DAL.Services.Interfaces;
using Silverlight.Weblog.Common.IoC;
using Silverlight.Weblog.Server.DAL;

namespace Silverlight.Weblog.Client.DAL.Services
{
    public class CategoryBlogPostService : ICategoryBlogPostService
    {
        [Dependency]
        public IDBContextContainer DBContextContainer { get; set; }

        public void DisplayCategory(string CategoryName, string Username)
        {
            DBContextContainer.Current.Load(
             DBContextContainer.Current.GetBlogPostsInCategoryQuery(CategoryName, Username),
             initial =>
                 {
                     IoC.Get<IEventAggregator>().GetEvent<GoToBlogPostsEvent>().Publish(initial.Entities.ToList());

                     if (!initial.Entities.Any())
                         IoC.Get<IEventAggregator>().GetEvent<ChangingTitleEvent>().Publish(string.Format("No blog posts under \"{0}\"", CategoryName));
               
                 }
             , null);

            IoC.Get<IEventAggregator>().GetEvent<ChangingTitleEvent>().Publish(CategoryName + " Blog Posts");
        }
    }
}
